package cn.virens.cmpt.server.handler;

import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.virens.cmpt.server.channel.ChannelSimple;

public class ChannelWriteHandler implements CompletionHandler<Integer, ByteBuffer> {
	private final Logger logger = LoggerFactory.getLogger(ChannelWriteHandler.class);

	private final ChannelSimple channel;

	public ChannelWriteHandler(ChannelSimple channel) {
		this.channel = channel;
	}

	@Override
	public void completed(Integer result, ByteBuffer buffer) {
		if (channel.getDataWriteListener() != null) {
			channel.getDataWriteListener().onWrite(channel, buffer, result);
		}
	}

	@Override
	public void failed(Throwable exc, ByteBuffer buffer) {
		if (channel.getDataErrorListener() != null) {
			channel.getDataErrorListener().onError(channel, buffer, exc);
		}
	}

}
